package com.eliota.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eliota.common.Constants;
import com.eliota.common.R;
import com.eliota.entity.Dict;
import com.eliota.entity.Role;
import com.eliota.service.DictService;
import com.eliota.service.RoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MG
 * @since 2023-03-28
 */
@RestController
@RequestMapping("/dict")
public class DictController {

    @Resource
    private DictService dictService;

    /**
     * 新增
     * @param dict
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Dict dict){
        dict.setType("icon");
        return R.success(dictService.saveOrUpdate(dict));
    }

    @GetMapping("/list")
    public R findAll(){
        return R.success(dictService.list());
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id){
        boolean isDel = dictService.removeById(id);
        if(isDel){
            return R.success();
        }else {
            return R.error("删除失败");
        }
    }

    @PostMapping("/bath/del")
    public R deleteBath(@RequestBody List<Integer> ids){
        boolean b = dictService.removeBatchByIds(ids);
        if(b){
            return R.success(true,"删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    @GetMapping("/page")
    public R findPage(@RequestParam Integer pageNum,
                      @RequestParam Integer pageSize,
                      @RequestParam String dictName){
        Page<Dict> rolePage = new Page<>(pageNum, pageSize);
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.like("name",dictName).orderByDesc("id");

        return R.success(dictService.page(rolePage,dictQueryWrapper));
    }

    @GetMapping("/icons")
    public R getIcons(){
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return R.success(dictService.list(dictQueryWrapper));
    }

}
